package com.sxm.yiwei.activitis;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.adapters.AwardAdapter;
import com.sxm.yiwei.beans.Award;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class MsgAwardActivity extends BaseActivity implements View.OnClickListener{

    private ImageView awardback;
    private PullToRefreshListView awardlist;
    private SharedPreferences sharedPreferences;
    private String token;
    private String userId;
    private String appId;
    private int no = 1;
    private ImageView nodata;
    private ImageView backmain;
    List<Award.DataBean> list = new ArrayList<Award.DataBean>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_msg_award);
        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        token = sharedPreferences.getString("token", "");
        userId = sharedPreferences.getString("userId", "");
        appId = sharedPreferences.getString("appid", "");
        // 初始化控件
        initAwardView();
        getAwardList(no);
        AwardAdapter adapter = new AwardAdapter(MsgAwardActivity.this,list);
        awardlist.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        awardlist.setMode(PullToRefreshBase.Mode.BOTH);
        awardlist.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                no++;
                getAwardList(no);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                no++;
                getAwardList(no);
            }
        });
    }

    private void getAwardList(int no) {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.AWARDLIST);
        params.addHeader("Content-Type","application/json");
        params.addHeader("appId", appId);
        params.addHeader("token", token);
        params.addHeader("userId", userId);
        JSONObject obj = new JSONObject();
        obj.put("pageNo",no+"");
        obj.put("pageSize","20");
        params.addParameter("params",obj.toJSONString());
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                awardlist.onRefreshComplete();
                JSONObject obj = JSON.parseObject(result);
                String status = obj.getString("status");
                if (status.equals("1")){
                    getUtilData(result);
                }


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                awardlist.onRefreshComplete();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }

    private void getUtilData(String result) {
        JSONObject object = JSON.parseObject(result);
        JSONArray data = object.getJSONArray("data");

        if (data.size()==0){

        }else {
            nodata.setVisibility(View.GONE);
        }
        for (int i = 0; i < data.size(); i++) {
            Award.DataBean dataBean = new Award.DataBean();
            JSONObject obj1 = JSON.parseObject(String.valueOf(data.get(i)));
            String amount = obj1.getString("amount");
            String avatar = obj1.getString("avatar");
            String id = obj1.getString("id");
            String nickname = obj1.getString("nickname");
            String time = obj1.getString("time");
            dataBean.setTime(Long.parseLong(time));
            dataBean.setAmount(Integer.valueOf(amount));
            dataBean.setId(id);
            dataBean.setAvatar(avatar);
            dataBean.setNickname(nickname);
            list.add(dataBean);
        }

    }

    private void initAwardView() {
       awardback =  (ImageView) findViewById(R.id.message_awardback);
        awardlist = (PullToRefreshListView) findViewById(R.id.award_list);
        nodata = (ImageView) findViewById(R.id.msgaward_nodata);
        backmain = (ImageView) findViewById(R.id.msgaward_backmain);

        // 设置点击事件
        awardback.setOnClickListener(this);
        backmain.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.message_awardback:
                finish();
                break;
            case R.id.msgaward_backmain:
                startActivity(new Intent(MsgAwardActivity.this, MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                break;
        }
    }
}
